home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTLAST.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  100 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btlast.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btlast - last btree key
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btlast(btp)
  25.      btree_t *btp;
  26.  
  27. DESCRIPTION
  28.      The btlast function positions the cursor of btree btp on the last
  29.      key in that btree.
  30.  
  31.      btlast will fail if one or more of the following is true:
  32.  
  33.      [EINVAL]       btp is not a valid btree pointer.
  34.      [BTELOCK]      btp is not locked.
  35.      [BTENKEY]      btp is empty.
  36.      [BTENOPEN]     btp is not open.
  37.  
  38. SEE ALSO
  39.      btfirst, btkeycnt, btnext, btprev.
  40.  
  41. DIAGNOSTICS
  42.      Upon successful completion, a value of 0 is returned.  Otherwise,
  43.      a value of -1 is returned, and errno set to indicate the error.
  44.  
  45. ------------------------------------------------------------------------------*/
  46. #ifdef AC_PROTO
  47. int btlast(btree_t *btp)
  48. #else
  49. int btlast(btp)
  50. btree_t *btp;
  51. #endif
  52. {
  53.     int terrno = 0;        /* tmp errno */
  54.  
  55.     /* validate arguments */
  56.     if (!bt_valid(btp)) {
  57.         errno = EINVAL;
  58.         return -1;
  59.     }
  60.  
  61.     /* check if not open */
  62.     if (!(btp->flags & BTOPEN)) {
  63.         errno = BTENOPEN;
  64.         return -1;
  65.     }
  66.  
  67.     /* check locks */
  68.     if (!(btp->flags & BTLOCKS)) {
  69.         errno = BTELOCK;
  70.         return -1;
  71.     }
  72.  
  73.     /* set cursor to last key */
  74.     btp->cbtpos.node = btp->bthdr.last;
  75.  
  76.     /* check if tree is empty */
  77.     if (btp->cbtpos.node == NIL) {
  78.         btp->cbtpos.key = 0;
  79.         bt_ndinit(btp, btp->cbtnp);
  80.         errno = BTENKEY;
  81.         return -1;
  82.     }
  83.  
  84.     /* read current node */
  85.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  86.         BTEPRINT;
  87.         terrno = errno;
  88.         btp->cbtpos.node = NIL;
  89.         btp->cbtpos.key = 0;
  90.         bt_ndinit(btp, btp->cbtnp);
  91.         errno = terrno;
  92.         return -1;
  93.     }
  94.  
  95.     /* set cursor to last key in node */
  96.     btp->cbtpos.key = btp->cbtnp->n;
  97.  
  98.     return 0;
  99. }
  100.